//
//  GoodInfoServiceCell.m
//  B2C
//
//  Created by xiaowu on 2017/7/31.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "GoodInfoServiceCell.h"

// 每个item之间的间隔大小
static CGFloat itemLeftOffset = 6;

@implementation GoodInfoServiceItemView

+ (CGFloat)getWidthStr:(NSString *)text {
    CGFloat width = 0;
    NSDictionary *attributes = @{NSFontAttributeName:TextNormalFont12};
    CGFloat textWidth = [text boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
    
    width += ceilf(textWidth);
    width += itemLeftOffset + 12 + 3;
    
    return width;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor grayfaColor];
        [self addSubview:self.imageView];
        [self addSubview:self.titleLabel];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    self.imageView.frame = CGRectMake(itemLeftOffset, (self.height - self.imageView.height) * 0.5, self.imageView.width, self.imageView.height);
    //    self.imageView.center = CGPointMake(15 + self.imageView.width * 0.5, self.height * 0.5);
    
    self.titleLabel.frame = CGRectMake(itemLeftOffset + 12 + 3, 0, self.width - self.imageView.right - 3, self.height);
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.imageView.frame = CGRectMake(itemLeftOffset, (self.height - self.imageView.height) * 0.5, self.imageView.width, self.imageView.height);
    
    self.titleLabel.frame = CGRectMake(itemLeftOffset + 12 + 3, 0, self.width - self.imageView.right - 3, self.height);
}

#pragma mark - Getters
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.image = [UIImage imageNamed:@"good_sign"];
        _imageView.size = CGSizeMake(12, 12);
    }
    return _imageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = TextNormalFont12;
        _titleLabel.textColor = [UIColor gray66Color];
    }
    return _titleLabel;
}

@end


@interface GoodInfoServiceCell ()
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) UIImageView *arrowImageView;
@end

@implementation GoodInfoServiceCell

- (instancetype)init {
    self = [super init];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _arr = [NSMutableArray array];
        self.backgroundColor = [UIColor grayfaColor];
        self.contentView.backgroundColor = [UIColor grayfaColor];
//        [self.contentView addSubview:self.arrowImageView];
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return self;
}


- (void)setValueForCellWithStrArr:(NSArray *)strArr {
    for (UIView *view in self.contentView.subviews) {
        if ([view isKindOfClass:[GoodInfoServiceItemView class]]) {
            [view removeFromSuperview];
        }
    }
    self.arr = [NSMutableArray arrayWithArray:strArr];
    CGFloat width = 15 - itemLeftOffset;
    
    for (NSString *str in strArr) {
        GoodInfoServiceItemView *itemView = [[GoodInfoServiceItemView alloc] init];
        itemView.frame = CGRectMake(width, 0, [GoodInfoServiceItemView getWidthStr:str], self.contentView.height);
        itemView.titleLabel.text = str;
        width += itemView.width;
        if (width > self.contentView.width - 15) {
            itemView.width = self.contentView.width - itemView.mj_x;
        }
        [self.contentView addSubview:itemView];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (UIImageView *)arrowImageView {
    if (!_arrowImageView) {
        _arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"good_fill"]];
        _arrowImageView.size = CGSizeMake(6, 13);
    }
    return _arrowImageView;
}

@end
